<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">名称</label>
        <el-input v-model="query.name" clearable placeholder="名称" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">实验室</label>
        <el-select
          v-model="query.labId"
          clearable
          style="width: 185px;"
          placeholder="实验室"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        >
          <el-option
            v-for="item in labs"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
        <!-- <label class="el-form-item-label">负责人</label>
        <el-input v-model="query.administrator" clearable placeholder="负责人" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" /> -->
           <label class="el-form-item-label">时间</label>
        <date-range-picker v-model="query.createdAt" class="date-item" />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px" />
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <!-- <el-table-column type="selection" width="55" /> -->
        <el-table-column prop="name" label="名称"  align="center"/>
        <el-table-column prop="labName" label="实验室"  align="center"/>
        <el-table-column prop="adminName" label="负责人"  align="center"/>
        <el-table-column prop="temperature" label="温度" align="center">
          <template slot-scope="scope">
            {{ scope.row.temperature }}°C
          </template>
        </el-table-column>
        <el-table-column prop="temperatureThresholdValue" label="温度阈值" align="center">
          <template slot-scope="scope">
            {{ scope.row.temperatureThresholdValue }}°C
          </template>
        </el-table-column>
        <el-table-column prop="humidity" label="湿度" align="center">
          <template slot-scope="scope">
            {{ scope.row.humidity }}%
          </template>
        </el-table-column>
        <el-table-column prop="humidityThresholdValue" label="湿度阈值" align="center">
          <template slot-scope="scope">
            {{ scope.row.humidityThresholdValue }}%
          </template>
        </el-table-column>
        <el-table-column prop="pressure" label="压差" align="center">
          <template slot-scope="scope">
            {{ scope.row.pressure }}Pa
          </template>
        </el-table-column>
        <el-table-column prop="pressureThresholdValue" label="压差阈值" align="center">
          <template slot-scope="scope">
            {{ scope.row.pressureThresholdValue }}Pa
          </template>
        </el-table-column>
        <el-table-column prop="errorMessage" label="报警信息"  align="center"/>
        <el-table-column prop="createdAt" label="时间"  align="center"/>
        <!-- <el-table-column v-if="checkPer(['admin','environmentMonitorLog:edit','environmentMonitorLog:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column> -->
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudEnvironmentMonitorLog ,{searchId}from '@/api/environmentMonitorLog'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { get } from '@/api/lab'
import DateRangePicker from '@/components/DateRangePicker'
import { mapState } from 'vuex'

const defaultForm = { id: null, environmentMonitorId: null, name: null, labId: null, administrator: null, temperature: null, temperatureThresholdValue: null, humidity: null, humidityThresholdValue: null, pressure: null, pressureThresholdValue: null, errorMessage: null, createdAt: null, updatedAt: null }
export default {
  name: 'EnvironmentMonitorLog',
  components: { pagination, crudOperation, rrOperation, udOperation, DateRangePicker },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: 'environment-monitor-log', url: 'api/environmentMonitorLog', idField: 'id', sort: 'id,desc', crudMethod: { ...crudEnvironmentMonitorLog },
      optShow: {
      add: false,
      edit: false,
      del: false,
      download: true,
      reset: true
    },})
  },
  data() {
    return {
      permission: {
        add: ['admin', 'environmentMonitorLog:add'],
        edit: ['admin', 'environmentMonitorLog:edit'],
        del: ['admin', 'environmentMonitorLog:del']
      },
      rules: {
      },
      queryTypeOptions: [
        { key: 'name', display_name: '名称' },
        { key: 'labId', display_name: '实验室' },
        { key: 'administrator', display_name: '负责人' },
        { key: 'createdAt', display_name: '时间' }
      ],
      labs: [],
    }
  },
  created() {
    this.getLabs()
  },
  computed: {
    ...mapState('hjuser', ['systemSearchInfoId'])
  },
  methods: {
        getLabs() {
      get()
        .then(response => {
          this.labs = response
        })
        .catch(_ => {
          console.log(response)
        })
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    dateFormat(picker) {
      if (picker == null) {
        this.form.createdAt = null
        this.form.updatedAt = null
      } else {
        this.form.createdAt = picker[0]
        this.form.updatedAt = picker[1]
      }
    },
    async [CRUD.HOOK.afterRefresh]() {
      if (this.systemSearchInfoId) {
        try {
          const res = await searchId(this.systemSearchInfoId)
        this.crud.data = res.content
        } finally {
          this.$store.commit('hjuser/updatedsystemSearchInfoId', '')
        }
      }
      return true
    },
  }
}
</script>

<style scoped>
/* .head-container .el-form-item-label {
    margin: 0px 3px 0px 0;
    display: inline-block;
    text-align: right;
    vertical-align: middle;
    font-size: 14px;
    color: #606266;
    line-height: 30.5px;
    padding: 0 7px 0 7px;
} */
</style>
